define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'editable'], function ($, undefined, Backend, Table, Form) {
    var Controller = {
        combo_open: () => {
            //========= 对科目名称进行搜索
            $(document).on("input propertychange","#subject",function () {
                let subjectName = $(this).val()
                if (subjectName.length == 0) $(".subject-id").val(0)
                $.post("yexam/subject/index?search="+subjectName+"&sort=id&order=desc&offset=0&limit=100&filter=%7B%7D&op=%7B%7D").then(response => {
                    var subjectList = response.rows;
                    let html = ''
                    subjectList.forEach(function (item) {
                        html += `<a href="#"><p data-id="${item.id}">${item.short_name}</p></a>`
                    })
                    $(".subject-list").html(html);
                })
            })
            //======= 对选择的科目进行搜索套餐
            $(document).on("click",'.subject-list p',function () {
                $("#subject").val($(this).html())
                let subjectId = $(this).data('id');
                $(".subject-id").val(subjectId)
                let html = ""
                $.get("yexam/user_service/getComboList",{subject_id: subjectId}).then(response => {
                    let  comboList = response.data;
                    comboList.forEach(function (item) {
                        html += `<option value="${item.combo_id}">${item.combo_name}</option>`
                    })
                    $(".combo-list").html(html)
                });
            })
            //======== 请求开通套餐
            $(".btn-embossed").click(function () {
                let comboInfo = "确认开通?";
                $.post("yexam/user_service/getUserComboInfo",$("form").serializeArray()).then(response => {
                    if (response.code > 0)
                    {
                        comboInfo = "您已开通以下套餐:"
                        comboInfo += response.data;
                        comboInfo += "是否要继续开通？"
                    }
                    Layer.confirm(comboInfo,function () {
                        $.post("yexam/user_service/requestOpenCombo",$("form").serializeArray()).then(response => {
                            Layer.msg(response.msg);
                        })
                    });
                })
            });
        },
        api: {
            formatter:{
                is_pay: function (value, row, index) {
                    return value?'是':'否';
                },
            },
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        },
        user_select: () => {
            Table.api.init({
                extend: {
                    index_url: 'yexam/user_service/index',
                    table: 'category',
                    edit_url: "yexam/user_service/edit"
                }
            })
            var table = $("#table");
            var tableOptions = {
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'pay_time',
                pagination: true,
                commonSearch: false,
                search: true,
                columns: [
                    [
                        //{checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'username', title: __('用户名'), operate: false, searchList: Config.searchList},
                        {field: 'mobile', title: __('手机号'), align: 'left',},
                        {field: 'short_name', title: __('科目')},
                        {field: 'combo_name', title: __('套餐'), align: 'left',operate: true},
                        {field: 'active_code', title: __('激活码')},
                        {field: 'pay_time', title: __('激活时间'), sortable: true},
                        {field: 'invalid_time', title: __('到期时间'), sortable: true},
                        {field: 'agent', title: __('归属代理'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate},
                        {field: 'remark', title: __('备注'),operate:'LIKE',editable: true}
                    ]
                ]
            };
            // 初始化表格
            table.bootstrapTable(tableOptions);
            Table.api.bindevent(table);
        }
    };
    return Controller;
});